<?php
/**
 * @author Jumper Swordwave
 * date: 2021/04/19
 */
declare(strict_types=1);
namespace App\Game\Commands;

use Psr\Container\ContainerInterface;
use App\Game\Core\{Request, Response, Command, GameStatus};
use App\Game\Service\{RoomService, UserService};
use App\Game\Task\GameTask;

/**
 * 系统消息反馈
 */
class SysCommand
{
    /**
     * @var UserService
     */
    private $userService;

    /**
     * @var RoomService
     */
    private $roomService;
    
    public function __construct(UserService $userService, RoomService $roomService)
    {
        $this->userService = $userService;
        $this->roomService = $roomService;
    }

    /**
     * 用户连接上服务器
     */
    public function connected(Request $req)
    {
        return Response::success(Command::SYS_CONNECTED, []);
    }

    /**
     * 玩家离线
     */
    public function disconnected(Request $req)
    {
        $user = $this->userService->findUser($req->getFd(), $req->getUser());
        if ($user) {
            $this->userService->disconnected($user);
        }
    }

    /**
     * 服务器心跳连接
     */
    public function heart(Request $req)
    {
        return Response::success(Command::SYS_HEART_RES, []);
    }

    /**
     * 用户断线重连
     */
    public function reconnect(Request $req)
    {
        $user = $this->userService->findUser($req->getFd(), $req->getUser());
        if ($user) {
            $user->fd = $req->getFd();
            $this->userService->saveUser($user);
        } else {
            $udata = $req->getUser();
            $user = $this->userService->newUser($req->getFd(), $udata);
        }

        $dis_time = time() - $user->updated_at;
        if ($dis_time >= config('game.ddz.dis_time', 300)) {    //掉线超时
            $user->reset();
            $this->userService->saveUser($user);
        }

        if ($user->room <= 0) { //用户不在房间内
            return Response::success(Command::SYS_LOGIN, ['user' => $user->toArray()]);
        }

        $task = container()->get(GameTask::class);
        $room = $this->roomService->findRoom($user->room);
        if ($room == null) {
            return Response::success(Command::SYS_LOGIN, ['user' => $user->toArray()]);
        }
        if ($room->status == GameStatus::ROOM_CALL) {
            $this->userService->updateUser($user, ['status'=>GameStatus::USER_PLAYING]);
            $task->gameCall($room);
            return null;
        }
        
        if ($room->status == GameStatus::ROOM_PLAYING) {
            $this->userService->updateUser($user, ['status'=>GameStatus::USER_PLAYING]);
            $task->outCard($room);
            return null;
        }
        
        if ($room->status == GameStatus::ROOM_GAMEOVER) {
            $this->userService->updateUser($user, ['status'=>GameStatus::USER_GAMEOVER]);
            $task->gameover($room);
            return null;
        }

        if ($room->status == GameStatus::ROOM_WAITING) {
            $this->userService->updateUser($user, ['status'=>GameStatus::USER_WAITING]);
            $task->userReady($room);
        }
        
        return null;
    }

    /**
     * 用户登陆
     */
    public function login(Request $req)
    {
        $user = $this->userService->findUser($req->getFd(), $req->getUser());
        if ($user == null) {
            $user = $this->userService->newUser($req->getFd(), ['nickname' => $req->input('nickname')]);
        }
        
        return Response::success(Command::SYS_LOGIN, ['user' => $user->toArray()]);
    }

    /**
     * 用户退出
     */
    public function logout(Request $req)
    {
        //todo 须增加状态判断
        //$r = $this->userService->logout($req->getFd(), $req->getUser());
        return Response::success(Command::SYS_LOGOUT, ['result' => $r]);
    }
}